<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <title>SqlMan/Paging Query</title>
    <meta name="description" content="This tutorial will guide you through the process of paging database data in SqlMan using a sample test code."/>
    <meta name="keywords" content="SqlMan, document, java sql, java jdbc, java orm, jdbc tools, jdbc helper, paging query"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <link rel="stylesheet" href="https://framework.ajaxjs.com/static/new-ui/css/common.css"/>
    <link rel="stylesheet" href="https://iam.ajaxjs.com/asset/main.css"/>
    <link rel="icon" type="image/png" href="/asset/favicon.png"/>
    <script src="https://framework.ajaxjs.com/static/aj-docs/common.js"></script>
    <script>
            // 获取用户的默认语言
            var userLang = navigator.language || navigator.userLanguage;

            // 检查是否为中文环境（包括简体和繁体）
            if (userLang.startsWith('zh') && location.pathname.indexOf('cn') == -1) {
                 confirm('欢迎！您可以改为访问中文内容。是否继续？') && location.assign('/docs/cn');  // 如果是中文，则弹出提示
            }

            var _hmt = _hmt || [];
            (function() {
              var hm = document.createElement("script");
              hm.src = "https://hm.baidu.com/hm.js?3cb62106ad945fc91efddfe250e1542e";
              var s = document.getElementsByTagName("script")[0];
              s.parentNode.insertBefore(hm, s);
            })();

    </script>
</head>
<body>
<nav>
    <div>
        <div class="links">
            <a href="/">🏠 Home</a>
            |
            ⚙️ Source:
            <a target="_blank" href="https://github.com/lightweight-component/SqlMan">Github</a>/<a target="_blank" href="https://gitcode.com/lightweight-component/SqlMan">Gitcode</a>

            | <a href="/docs/cn">Chinese Version</a>
        </div>
        <h1>
            <img src="/asset/favicon-2x.png" style="vertical-align: middle;height: 45px;margin-bottom: 6px;"/> SqlMan
        </h1>
        <h3>Lightweight Data Access Tools -- User Manual
        </h3>
    </div>
</nav>
<div>
    <menu>

        <ul>
            <li class="selected">
                <a href="/docs">Home</a>
            </li>
            <li>
                <a href="/docs/quick-start">Quick Start</a>
            </li>
            <li>
                <a href="/docs/setup">Setup</a>
            </li>
        </ul>
        <h3>Querying the Database</h3>
        <ul>
            <li>
                <a href="/docs/query/query-concept">Query Concept</a>
            </li>
            <li>
                <a href="/docs/query/query-tutorial">Query Tutorial</a>
            </li>
            <li>
                <a href="/docs/query/query-paging">Paging Query</a>
            </li>
        </ul>
        <h3>Writing the data</h3>
        <ul>
            <li>
                <a href="/docs/write/Create-Update-Delete">Create/Update/Delete</a></li>
            <li>
                <a href="/docs/write/batch">Batch Update</a>
            </li>
        </ul>
        <h3>Other Usage</h3>
        <ul>
            <li>
                <a href="/docs/other/sql-xml">SQL in XML</a></li>
            <li>
                <a href="/docs/other/entity">No SQL but Entity</a>
            </li>
        </ul>
        <h3>Misc</h3>
        <ul>
            <li><a href="/docs/common/versions">Release History</a></li>
            <li><a href="/docs/common/contact">Contact</a></li>
        </ul>
    </menu>
    <article class="aj-text">
        <h1>Paging Query</h1>
        <p>Paging through large datasets is a common requirement in applications to enhance performance and provide a better user experience. This tutorial will guide you through the process of paging database data in
            SqlMan using a sample test code.</p>
        <h2>Paging with Default Settings</h2>
        <p>The first paging example demonstrates how to retrieve paginated results with default settings: fecth the data between the first row to 9th row. We use the <code>page</code> method without any parameters:</p>
        <pre><code class="language-java">PageResult&lt;Map&lt;String, Object&gt;&gt; result = new Sql(conn).input(&quot;SELECT * FROM article&quot;).page();
</code></pre>
        <h2>Paging with Custom Page Size and Number</h2>
        <p>The second example shows how to retrieve paginated results by specifying the record start and limit, like MySQL paging query:</p>
        <pre><code class="language-java">PageResult&lt;Map&lt;String, Object&gt;&gt; result = new Sql(conn).input(&quot;SELECT * FROM article&quot;).page(3, 5);
</code></pre>
        <h3>Paging with Custom Class Mapping</h3>
        <p>The third example demonstrates how to map the results to a custom class (<code>Address</code>) and retrieve paginated results:</p>
        <pre><code class="language-java">PageResult&lt;Address&gt; result = new Sql(conn).input(&quot;SELECT * FROM shop_address&quot;).page(Address.class, 1, 2);
</code></pre>
        <h2>Checking for Empty Results</h2>
        <p>The fourth example shows how to handle scenarios where the paginated result is empty:</p>
        <pre><code class="language-java">PageResult&lt;Map&lt;String, Object&gt;&gt; result = new Sql(conn).input(&quot;SELECT * FROM shop_address&quot;).page(Address.class, 100, 2);
assertEquals(0, result.size());
</code></pre>
        <h1>Binding Parameters</h1>
        <p>Binding Parameters is also supported in paging query. The same way to do that.</p>
        <pre><code class="language-java">PageResult&lt;Map&lt;String, Object&gt;&gt; result = new Sql(conn).input(&quot;SELECT * FROM shop_address where stat = ?&quot;, 1).page();
</code></pre>
        <h1>Paging with Custom Database Vendor</h1>
        <p>The final example demonstrates how to set a custom database vendor (e.g., SQL Server) and retrieve paginated results:</p>
        <pre><code class="language-java">Sql sqlServer = new Sql(conn);
sqlServer.setDatabaseVendor(JdbcConstants.DatabaseVendor.SQL_SERVER);
PageResult&lt;Map&lt;String, Object&gt;&gt; result = sqlServer.input(&quot;SELECT * FROM article&quot;).page();
</code></pre>
        <p>In this case, you cannot use method chaining anymore.</p>
        <h1>JSQL Parser</h1>
        <p>We use the <a href="https://github.com/JSQLParser/JSqlParser">JSQL Parser library</a> to implement paging functionality. It makes it easier to parse SQL statements and extract information from them. It also
            provides a convenient way to generate SQL statements dynamically.</p>

    </article>
</div>
<footer>
    SqlMan, a part of <a href="https://framework.ajaxjs.com" target="_blank">AJ-Framework</a> open source. Mail:
    frank@ajaxjs.com, visit <a href="https://blog.csdn.net/zhangxin09" target="_blank">my blog(In Chinese)</a>.
    <br/>
    <br/>
    Copyright © 2025 Frank Cheung. All rights reserved.
    <script>
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?3cb62106ad945fc91efddfe250e1542e";
          var s = document.getElementsByTagName("script")[0];
          s.parentNode.insertBefore(hm, s);
        })();

    </script>
</footer>
</body>
</html>